package com.wnhz.work.jy.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.RankingAdapter;
import com.wnhz.work.jy.bean.RankingBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---排行榜
 */
public class RankingListActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ListView lv_ranking;

    private List<RankingBean> rankList = new ArrayList<>();
    private RankingAdapter mAdapter;
    private RankingBean bean;

    private TextView tv_number_rank;//我的排名
    private ImageView iv_head_ranking;//我的头像
    private TextView tv_name_ranking;//我的姓名
    private TextView tv_money_ranking;//我的消费
    private TextView tv_address_ranking;//我的银行

    private String ranking;
    private String headimg;
    private String name;
    private String allmoney;
    private String bank;


    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;
    ImageOptions options = new ImageOptions.Builder()
            //设置加载过程中的图片
            .setLoadingDrawableId(R.mipmap.ic_morentouxiang)
            //设置加载失败后的图片
            .setFailureDrawableId(R.mipmap.ic_morentouxiang)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(true)
            //设置支持gif
            .setIgnoreGif(false)
            .build();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    /*for (int i = 0; i < rankList.size(); i++) {
                        if (MyApplication.getInstance().userBean.getUserId().equals(rankList.get(i).getUserid())) {
                            bean = rankList.get(i);
                            tv_number_rank.setText(bean.getNum());
                            x.image().bind(iv_head_ranking, bean.getHead(), options);
                            tv_name_ranking.setText(bean.getName());
                            tv_money_ranking.setText(bean.getMoney());
                            tv_address_ranking.setText(bean.getAddress());
                        }
                    }*/
                    tv_number_rank.setText(ranking);
                    x.image().bind(iv_head_ranking, headimg, options);
                    tv_name_ranking.setText(name);
                    tv_money_ranking.setText(allmoney);
                    if (!"null".equals(bank)){
                        tv_address_ranking.setText(bank);
                    }else {
                        tv_address_ranking.setText("");
                    }

                    mAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_list);
        x.view().inject(this);
        setBlank();
        initData();
        initView();
    }

    private void setBlank() {
        if (rankList.size() == 0) {
            blank_tv.setText("快来霸占排行榜~");
            blank_img.setImageResource(R.mipmap.ic_paihangbangweikong);
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void initData() {
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("排行榜");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        lv_ranking = (ListView) findViewById(R.id.lv_ranking);
        tv_number_rank = (TextView) findViewById(R.id.tv_number_rank);//我的排名
        iv_head_ranking = (ImageView) findViewById(R.id.iv_head_ranking);//我的头像
        tv_name_ranking = (TextView) findViewById(R.id.tv_name_ranking);//我的姓名
        tv_money_ranking = (TextView) findViewById(R.id.tv_money_ranking);//我的消费
        tv_address_ranking = (TextView) findViewById(R.id.tv_address_ranking);//我的银行

        uploadByxUtils();

        mAdapter = new RankingAdapter(RankingListActivity.this, rankList);
        lv_ranking.setAdapter(mAdapter);
    }

    private static final String TAG = "RankingListActivity";

    private void uploadByxUtils() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        XUtil.Post(ConfigUtil.RANKING, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: ranking" + result);
                Log.i(TAG, "onSuccess: ranking" + MyApplication.getInstance().userBean.getUserId());
                try {
                    JSONObject json = new JSONObject(result);
                    JSONArray jsonArray = json.optJSONArray("information");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        RankingBean bean = new RankingBean();
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        String userid = jsonObject.optString("userid");
                        String name = jsonObject.optString("name");
                        String allmoney = jsonObject.optString("allmoney");
                        String bank = jsonObject.optString("bank");
                        String ranking = jsonObject.optString("ranking");
                        String headimg = jsonObject.optString("headimg");

                        bean.setUserid(userid);
                        bean.setName(name);
                        bean.setMoney(allmoney);
                        if (!"null".equals(bank)){
                            bean.setAddress(bank);
                        }else {
                            bean.setAddress("");
                        }

                        bean.setNum(ranking);
                        bean.setHead(headimg);

                        rankList.add(bean);
                    }

                    if (json.has("userInfo")) {
                        JSONObject jsonObject1 = json.optJSONObject("userInfo");

                        ranking = jsonObject1.optString("ranking");
                        headimg = jsonObject1.optString("headimg");
                        name = jsonObject1.optString("name");
                        allmoney = jsonObject1.optString("allmoney");
                        bank = jsonObject1.optString("bank");

                    }
                    setBlank();
                    mHandler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                setBlank();
                closeDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }
}
